package edu.gatech.cs2340.group7.wheresmystuffapp.presenters;

import android.widget.Toast;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.CustomDate;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DataManager;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DatabaseHelper;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.IReportItemPresenter;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Item;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.ItemLocation;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.LostItem;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.ReportLostItemView;

/**
 * Presenter for reporting a lost item view
 * @author tbowling3
 *
 */
public class ReportLostItemPresenter extends IReportItemPresenter {

	public ReportLostItemPresenter(ReportLostItemView view) {
		super(view);
	}

	/**
	 * Adds a lost item to the database
	 * @param itemName the name of the item
	 * @param description the item's description
	 * @param category the category of the item
	 * @param date the date that the item was lost
	 * @param location the location that the item was lost
	 * @param pic - unused for now
	 * @return -1 if there was an error adding the item to the database
	 */
	public long addItem(String itemName, String description, String category, ItemLocation il, String date, String reward, byte[] picture) {
		long out = -1;
		if (itemName == null){
			Toast.makeText(myView, "Please enter a name", Toast.LENGTH_SHORT).show();
			//myView.setAddItemResult("Please enter a name");
			return out;
		}
		if (description == null){
			Toast.makeText(myView, "Please enter a description", Toast.LENGTH_SHORT).show();
			//myView.setAddItemResult("Please enter a description");
			return out;
		}
		if(date == null){
			Toast.makeText(myView, "Please enter a date", Toast.LENGTH_SHORT).show();
			//myView.setAddItemResult("Please enter a date");
			return out;
		}
		if(il == null){
			Toast.makeText(myView, "Please select a location", Toast.LENGTH_SHORT).show();
			//myView.setAddItemResult("Please enter a location");
			return out;
		}
		
		if(category.equals("empty")){
			Toast.makeText(myView, "Please select a category", Toast.LENGTH_SHORT).show();
			//myView.setAddItemResult("Please select a category");
			return out;
		}
		if(reward == null){
			Toast.makeText(myView, "Please Enter a reward", Toast.LENGTH_SHORT).show();
			return out;
		}
			
			// dateLost = checkDate(dateLost);
			int rewardVal = convertReward(reward);
			CustomDate dateLost = new CustomDate(date);

			String username = DataManager.getCurrentAccountName();
			Item created = new LostItem(username, category, itemName,
					description, il, rewardVal, dateLost, picture);
			//out = DataManager.addItem(created);
			DatabaseHelper helper = new DatabaseHelper(myView);
			helper.addItem(created);

			//myView.setAddItemResult("Item Added!"); 
			Toast.makeText(myView, "Item Added", Toast.LENGTH_SHORT).show();
		return out;
	}

}
